import { computed, defineComponent, onMounted, reactive, ref } from "vue";
import Page from "@/components/public/page/page";
import { CardWithTitle } from "@/components/public/card/card";
import LTable from "@/components/public/lTable/lTable";
import {
  DeleteOutlined,
  EditOutlined,
} from "@ant-design/icons-vue";
import insuranceTypeAttributeService from "@/modules/systemConfiguration/attributeManagement/insuranceTypeAttribute/insuranceTypeAttribute.service";
import EditPopup from "./editPopup/editPopup";
import { Modal } from "ant-design-vue";
export default defineComponent({
  name: 'insuranceTypeAttribute',
  setup() {
    const state = reactive({
      toolbar: {
        left: ["refresh", "add"],
        right: ["filter"],
        add: {
          handler: () => {
            editRef.value.open();
          }
        }
      },
      dataSource: [],
      columns: [
        {
          title: "编号",
          dataIndex: "id",
          align: "center",
          width: "100px",
        },
        {
          title: "属性名称",
          dataIndex: "name",
          align: "center",
          width: "200px",
        },
        {
          title: "属性编码",
          dataIndex: "code",
          align: "center",
          width: "200px",
        },
        {
          title: "属性类型",
          dataIndex: "type",
          align: "center",
          width: "200px",
        },
        {
          fixed: "right",
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "100px",
        },
      ],
      loading: false,
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
      },
    } as any);
    const LTableRef = ref(null as any);
    const $service = new insuranceTypeAttributeService();
    const editRef = ref(null as any);
    //删除
    const deleteRow = (id: number) => {
      Modal.confirm({
        title: "提示",
        content: "确认删除?",
        onOk: async () => {
          console.log(123)
          const del_res = await LTableRef.value.$deleteTableRows([id]);
          if (del_res) {
            await LTableRef.value.$fetchTableData();
          }
        },
      });
    };
    // 表格状态变更
    const tableChangeHandler = (pag: { pageSize: number; current: number }) => {
      state.pagination.current = pag.current;
      state.pagination.pageSize = pag.pageSize;
    };
    return () => (
      <Page>
        <CardWithTitle title="险种属性">
          <LTable
            ref={LTableRef}
            toolbar={state.toolbar}
            service={$service}
            v-model={state}
          >
            <a-table
              bordered={true}
              scroll={{ x: "max-content" }}
              dataSource={state.dataSource}
              columns={state.columns}
              pagination={state.pagination}
              loading={state.loading}
              onChange={tableChangeHandler}
              v-slots={{
                bodyCell: ({ column, record }: any) => {
                  //操作
                  if (column.dataIndex === "operation") {
                    return (
                      <div class="operation">
                        <a-button
                          type="primary"
                          size="small"
                          v-slots={{
                            icon: () => <EditOutlined />,
                          }}
                          onClick={() => {
                            editRef.value.open(record);
                          }}
                        >
                          编辑
                        </a-button>
                        <a-button
                          type="primary"
                          danger
                          size="small"
                          v-slots={{
                            icon: () => <DeleteOutlined />,
                          }}
                          onClick={() => deleteRow(record.id)}
                        >删除</a-button>
                      </div>
                    )
                  }
                },
              }}
            />
          </LTable>
        </CardWithTitle>
        <EditPopup
          ref={editRef}
          service={$service}
          onConfirm={()=>{ LTableRef.value.$fetchTableData(); }}
        />
      </Page>
    )
  }
})